<?php


namespace app\badmin\controller;


use app\badmin\model\Company;
use app\badmin\model\CompanyAgentStatistics;
use app\badmin\model\CompanyShop;
use app\badmin\model\CompanyShop as CompanyShopModel;
use app\badmin\model\CompanyShopStatistics;
use app\badmin\model\CompanyStatistics;
use app\badmin\model\CompanyUser as CompanyUserModel;
use app\common\controller\BadminController;
use think\Exception;

class Index extends BadminController {

    /**
     * 账号信息
     * @return false|string
     */
    public function getUserInfo() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            if ($params['account_type'] == 0) {//公司
                $company = Company::where('id', $params['company_id'])
                    ->field('name,f_phone as phone,default_city_code,logo')
                    ->find();
                $default_city_code = explode('-', $company['default_city_code']);
                $res['account_type'] = 0;
                $res['city_code'] = $default_city_code[0];
                $res['city_name'] = $default_city_code[1];
                $res['company_name'] = $company['name'];
                $res['my_name'] = $company['name'];
                $res['phone'] = $company['phone'];
                $res['logo'] = $company['logo'];
            } else {
                $companyShop = CompanyShopModel::where('id', $params['company_shop_id'])
                    ->field('name,phone,city_code')
                    ->find();
                $company = Company::where('id', $params['company_id'])
                    ->field('name,logo')
                    ->find();
                $default_city_code = explode('-', $companyShop['city_code']);
                $res['account_type'] = 1;
                $res['city_code'] = $default_city_code[0];
                $res['city_name'] = $default_city_code[1];
                $res['company_name'] = $company['name'];
                $res['my_name'] = $companyShop['name'];
                $res['phone'] = $companyShop['phone'];
                $res['logo'] = $company['logo'];
            }
            return $this->resData('success', $res);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     *首页统计
     * @return false|string
     */
    public function homepage() {
        try {
            $params = $this->checkParams('start_time,end_time');
            if ($params['account_type'] == 0) {//公司账号
                $res1 = CompanyStatistics::yesterdayUser($params);//昨日新增潜在
                $res2 = CompanyStatistics::totalUser($params);//累计新增潜在
                list($lave_num, $useAgentNum) = Company::agentNum($params); //已使用,未使用经纪人数
                $line1 = CompanyStatistics::timeLine($params, 1);//新增潜在用户时间线
                $line2 = CompanyStatistics::timeLine($params, 2);//新增专属用户时间线
            } else {
                $res1 = CompanyShopStatistics::yesterdayUser($params);//昨日新增潜在
                $res2 = CompanyShopStatistics::totalUser($params);//累计新增潜在
                list($lave_num, $useAgentNum) = CompanyShopModel::agentNum($params);//已使用,未使用经纪人数
                $line1 = CompanyShopStatistics::timeLine($params, 1);//新增潜在用户时间线
                $line2 = CompanyShopStatistics::timeLine($params, 2);//新增专属用户时间线
            }
            $res3 = CompanyAgentStatistics::getList($params);//经纪人累计专属客户排行榜
            $data = [
                'yesterdayUser' => $res1,
                'totalUser'     => $res2,
                'useAgentNum'   => $useAgentNum,
                'lave_num'      => $lave_num,
                'line'          => compact('line1', 'line2', 'date'),
                'res3'          => $res3,
            ];
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}